package tech.aistar.day11;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * 本类用来演示: 读写锁 - 锁的升级和降级
 *
 * @author: success
 * @date: 2022/8/2 11:04 上午
 */
public class DemoteDemo {
    public static void main(String[] args) {
        //定义一个读写锁
        ReadWriteLock rwLock = new ReentrantReadWriteLock();

        //获取读锁
        Lock rLock = rwLock.readLock();

        //获取写锁
        Lock wLock = rwLock.writeLock();

        //读
        rLock.lock();
        System.out.println("读锁begin working");

        //在没有释放读锁之前,想要直接加一个写锁

        //结论:读锁如果没有得到释放,是不能够继续加写锁的.

        //加写锁
        wLock.lock();
        System.out.println("写锁begin working");

        //释放锁
        wLock.unlock();
        rLock.unlock();
    }
}
